# -*- coding： UTF-8 -*-
from random import  randint
from paper.Expression import Expression
from util.math.MathUtil import randomFraction


class PaperFactory:

    def __init__(self,arguments):
        self.range = arguments["range"]
        self.numbers = arguments["number"]
        self.operates = ['+','-','*','÷']

    def createPaper(self):
        paper = []
        problemLen = self.numbers
        i = 1
        while i <= problemLen:
            problem,checkNegative = self.createProblem()
            if checkNegative :
               if problem.isNegative() :
                   continue

            if self.checkRepeat(problem.getHashCode()):
                continue
            paper.append(problem)
            i = i + 1
        return  paper

    def checkRepeat(self,expression):
        #查重
        return False

    def createProblem(self):
        pass
        r = self.range
        operates = self.operates

        numLen = randint(2,4)
        expression = []
        flag = False
        expLen = numLen * 2 - 1
        mayBeNegative = False

        # 分数生成机率:20% 整数生成机率:80%
        # 括号在输出时生成
        for i in range(expLen):
            if flag :
                # 生成操作符
                flag = False
                index =randint(0,3)
                if index == 1 : mayBeNegative = True
                expression.append(operates[index])
            else:
                # 生成数字
                flag = True
                if randint(1,10) <= 8:
                    expression.append(randint(1,r))
                else:
                    expression.append(randomFraction(r))
        return Expression(expression),mayBeNegative